package com.jt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.service.ItemService;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemDescMapper itemDescMapper;


	/**
	 * 业务说明：分页查询所有的商品目录，封装到EasyUITable中
	 * @param page :当前页数
	 * @param rows :每页显示行数
	 * */
	@Override
	public EasyUITable<Item> getAllItem(Integer page, Integer rows) {
		QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
		queryWrapper.orderByDesc("updated");
		Page<Item> itemPage=new Page<>(page,rows);
		itemPage=itemMapper.selectPage(itemPage,queryWrapper);
		return new EasyUITable<>(itemPage.getTotal(),itemPage.getRecords());

	}

	/**
	 * 业务说明：更新商品
	 * @param item 所需要更新的商品信息
	 * */
	@Override
	public Integer updateItemById(Item item) {
		//item.setUpdated(new Date());
		return itemMapper.updateById(item);
	}

	/**
	 * 业务说明：往数据库中写入新的商品数据
	 * @param item ：新的item内容
	 * @param itemDesc*/
	@Override
	@Transactional
	public Integer saveItem(Item item, String itemDesc) {
		//Date date=new Date();
		//item.setCreated(date).setUpdated(date);
		itemMapper.insert(item);
		ItemDesc itemDesc1=new ItemDesc().setItemDesc(itemDesc).setItemId(item.getId());
		return itemDescMapper.insert(itemDesc1);
	}

	/**
	 * 业务说明：删除对应的商品类
	 * @param ids :客户端传入的id集合
	 * */
	@Override
	public Integer deleteItemByIds(Long... ids) {
		return itemMapper.deleteBatchIds(Arrays.asList(ids));
	}

	@Override
	public String updateStatus(Integer status, Long... ids) {
		itemMapper.updateStatus(status,ids);
		return status==1?"上架成功":"下架成功";
	}



	/**
	 * 业务说明：将商品状态改为下架状态
	 * @param ids :客户端传入的id集合
	 * */
/*	@Override
	public String updateStatus(Integer status,Long... ids) {
		Item item=new Item();
		item.setStatus(status);
		QueryWrapper<Item> queryWrapper=new QueryWrapper<>();
		queryWrapper.in("id",ids);
		itemMapper.update(item,queryWrapper);
		return status==1?"上架成功":"下架成功";
	}*/





}
